package com.cms.sync.action;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.action.DefaultAction;
import com.cms.model.AppInfo;
import com.cms.model.AppVersionInfo;
import com.cms.model.Channel;
import com.cms.model.TerraceInfo;
import com.cms.service.AppInfoServiceImpl;
import com.cms.service.AppVersionInfoServiceImpl;
import com.cms.service.ChannelService;
import com.cms.service.ChannelServiceImpl;
import com.cms.service.TerraceInfoServiceImpl;
import com.cms.sync.model.AllQueryData;
import com.cms.sync.model.KpayFeeData;
import com.cms.sync.model.KpayKeepAndUserNum;
import com.cms.sync.service.KpayActionServiceImpl;
import com.cms.sync.service.KpayKeepServiceImpl;
import com.cms.sync.service.KpaySyncLogServiceImpl;
import com.model.Admin;
import com.util.Util;

public class KpaySyncAction extends DefaultAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -658324989232813182L;
	
	String startday;
	String endday;

	String dimension;

	Map<Integer,Channel> channellist;
	ChannelService channelservice = new ChannelServiceImpl();
	
	String type;
	float totalreqfee;
	int totalreqsum;
	float totalbillfee;
	int totalbillsum;
	
	KpayFeeData feedata = new KpayFeeData();
	List<KpayFeeData> feelist;
	List<TerraceInfo> providerlist;
	Map<String,AppVersionInfo> appversion;
	
	Map<String,Integer> totalmap = new HashMap<String,Integer>();
	KpayKeepAndUserNum keepandusernum = new KpayKeepAndUserNum();
	List<KpayKeepAndUserNum> keepandusernumlist;
	
	List<AppInfo> applist;
	List<AppVersionInfo> appversionlist;
	List<Channel> channellist1;
	AllQueryData alldata = new AllQueryData();
	List<AllQueryData> alldatalist;
	private String appnumber;
	
	public String feeQuery(){
		providerlist = new TerraceInfoServiceImpl().getTerraceInfos();
		if(startday==null||startday.equals("")){
			Calendar c = Calendar.getInstance();
			startday = endday= Util.dateToString(c.getTime(), "yyyyMMdd");
		}
		if("query".equals(type))
		{
			Admin admin = getAdmin();
			feelist = new KpaySyncLogServiceImpl().getKpayFeeData(startday,endday, feedata,dimension,admin);
			if(feelist!=null)
			{
				for(int i = 0;i<feelist.size();i++)
				{
				KpayFeeData fd = feelist.get(i);
					totalreqfee += fd.getReqfee();
					totalreqsum+=fd.getReqsum();
					totalbillfee += fd.getBillfee();
					totalbillsum+=fd.getBillsum();
				}
			}
			//游戏版本列表
			if(dimension.indexOf("app")!=-1){
				appversion = new HashMap<String,AppVersionInfo>();
				for (AppVersionInfo item : new AppVersionInfoServiceImpl().getAppVersionInfos()) {
					appversion.put(item.getAppversionnumber(), item);
				}
			}
			//渠道列表
			if(dimension.indexOf("channel")!=-1){
				channellist = new HashMap<Integer,Channel>();
				for (Channel item : channelservice.getChannels()) {
					channellist.put(item.getId(), item);
				}
			}
		}
		return SUCCESS;
	}
	public String userQuery(){
		if(startday==null||startday.equals("")){
			Calendar c = Calendar.getInstance();
			startday = endday= Util.dateToString(c.getTime(), "yyyyMMdd");
		}
		if("query".equals(type))
		{
			Admin admin = getAdmin();
			keepandusernumlist = new KpayKeepServiceImpl().getkeepandusernum(startday, endday, keepandusernum, dimension,admin);
			if(keepandusernumlist!=null)
			{
				for(int i = 0;i<keepandusernumlist.size();i++)
				{
					KpayKeepAndUserNum k = keepandusernumlist.get(i);
					if(!totalmap.containsKey("totalnewuser"))
						totalmap.put("totalnewuser", 0);
					totalmap.put("totalnewuser",k.getNewuser()+totalmap.get("totalnewuser"));
					
					if(!totalmap.containsKey("totalactiveuser"))
						totalmap.put("totalactiveuser", 0);
					totalmap.put("totalactiveuser",k.getActiveuser()+totalmap.get("totalactiveuser"));
					
					if(!totalmap.containsKey("totalnextdaykeep"))
						totalmap.put("totalnextdaykeep", 0);
					totalmap.put("totalnextdaykeep",k.getNextdaykeep()+totalmap.get("totalnextdaykeep"));
					
					if(!totalmap.containsKey("totaltwodaykeep"))
						totalmap.put("totaltwodaykeep", 0);
					totalmap.put("totaltwodaykeep",k.getTwodaykeep()+totalmap.get("totaltwodaykeep"));
					
					if(!totalmap.containsKey("totalthreedaykeep"))
						totalmap.put("totalthreedaykeep", 0);
					totalmap.put("totalthreedaykeep",k.getThreedaykeep()+totalmap.get("totalthreedaykeep"));
					
					if(!totalmap.containsKey("totalfourdaykeep"))
						totalmap.put("totalfourdaykeep", 0);
					totalmap.put("totalfourdaykeep",k.getFourdaykeep()+totalmap.get("totalfourdaykeep"));
					
					if(!totalmap.containsKey("totalfivedaykeep"))
						totalmap.put("totalfivedaykeep", 0);
					totalmap.put("totalfivedaykeep",k.getFivedaykeep()+totalmap.get("totalfivedaykeep"));
					
					if(!totalmap.containsKey("totalsixdaykeep"))
						totalmap.put("totalsixdaykeep", 0);
					totalmap.put("totalsixdaykeep",k.getSixdaykeep()+totalmap.get("totalsixdaykeep"));
					
					if(!totalmap.containsKey("totalsevendaykeep"))
						totalmap.put("totalsevendaykeep", 0);
					totalmap.put("totalsevendaykeep",k.getSevendaykeep()+totalmap.get("totalsevendaykeep"));
					//"totaltendaykeep","totalfifteendaykeep","totalthirtydaykeep"
					if(!totalmap.containsKey("totaltendaykeep"))
						totalmap.put("totaltendaykeep", 0);
					totalmap.put("totaltendaykeep",k.getTendaykeep()+totalmap.get("totaltendaykeep"));
					
					if(!totalmap.containsKey("totalfifteendaykeep"))
						totalmap.put("totalfifteendaykeep", 0);
					totalmap.put("totalfifteendaykeep",k.getFifteendaykeep()+totalmap.get("totalfifteendaykeep"));
					
					if(!totalmap.containsKey("totalthirtydaykeep"))
						totalmap.put("totalthirtydaykeep", 0);
					totalmap.put("totalthirtydaykeep",k.getThirtydaykeep()+totalmap.get("totalthirtydaykeep"));
				}
			}
			//游戏版本列表
			if(dimension.indexOf("app")!=-1){
				appversion = new HashMap<String,AppVersionInfo>();
				for (AppVersionInfo item : new AppVersionInfoServiceImpl().getAppVersionInfos()) {
					appversion.put(item.getAppversionnumber(), item);
				}
			}
			//渠道列表
			if(dimension.indexOf("channel")!=-1){
				channellist = new HashMap<Integer,Channel>();
				for (Channel item : channelservice.getChannels()) {
					channellist.put(item.getId(), item);
				}
			}
		}
		return SUCCESS;
	}
	public String allQuery(){
		applist = new AppInfoServiceImpl().getAppInfos();
		channellist1 = channelservice.getChannels();
		if(startday==null||startday.equals("")){
			Calendar c = Calendar.getInstance();
			startday = Util.dateToString(c.getTime(), "yyyyMM")+"01";
			endday= Util.dateToString(c.getTime(), "yyyyMMdd");
		}
		if("query".equals(type))
		{
			alldatalist = new KpayActionServiceImpl().getAllData(startday, endday, alldata, dimension);
			
			appversionlist =  new AppVersionInfoServiceImpl().getAppVersionInfoByAppNumber(appnumber);
			
			//游戏版本列表
			if(dimension.indexOf("app")!=-1){
				appversion = new HashMap<String,AppVersionInfo>();
				for (AppVersionInfo item : new AppVersionInfoServiceImpl().getAppVersionInfos()) {
					appversion.put(item.getAppversionnumber(), item);
				}
			}
			//渠道列表
			if(dimension.indexOf("channel")!=-1){
				channellist = new HashMap<Integer,Channel>();
				for (Channel item : channelservice.getChannels()) {
					channellist.put(item.getId(), item);
				}
			}
		}
		return SUCCESS;
	}
	public String changeappversion(){
		appversionlist = new AppVersionInfoServiceImpl().getAppVersionInfoByAppNumber(appnumber);
		return SUCCESS;
	}
	public String formatfee(String fee)
	{
		if(fee.endsWith(".0"))
		{
			fee = fee.substring(0,(fee.length()-2));
		}
		DecimalFormat format = new DecimalFormat("#.00");
		return format.format(Double.parseDouble(fee));
	}
	public String computearpu(float fee,int activeuser)
	{
		String arpu = "--";
		if(activeuser >0 && fee>0)
		{
			float f = fee/activeuser;
			DecimalFormat format = new DecimalFormat("00.00");
	        arpu = format.format(f*100)+"%";
		}
		return arpu;
	}

	public String computearpu1(float fee,int activeuser)
	{
		String arpu = "--";
		if(activeuser >0 && fee>0)
		{
			float f = fee/activeuser;
			DecimalFormat format = new DecimalFormat("#.00");
	        arpu = format.format(f);
		}
		return arpu;
	}
	public String getStartday() {
		return startday;
	}
	public void setStartday(String startday) {
		this.startday = startday;
	}
	public String getEndday() {
		return endday;
	}
	public void setEndday(String endday) {
		this.endday = endday;
	}
	public List<KpayFeeData> getFeelist() {
		return feelist;
	}
	public void setFeelist(List<KpayFeeData> feelist) {
		this.feelist = feelist;
	}
	
	public Map<String, AppVersionInfo> getAppversion() {
		return appversion;
	}
	public void setAppversion(Map<String, AppVersionInfo> appversion) {
		this.appversion = appversion;
	}
	public String getDimension() {
		return dimension;
	}
	public void setDimension(String dimension) {
		this.dimension = dimension;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public float getTotalreqfee() {
		return totalreqfee;
	}
	public void setTotalreqfee(float totalreqfee) {
		this.totalreqfee = totalreqfee;
	}
	public int getTotalreqsum() {
		return totalreqsum;
	}
	public void setTotalreqsum(int totalreqsum) {
		this.totalreqsum = totalreqsum;
	}
	public float getTotalbillfee() {
		return totalbillfee;
	}
	public void setTotalbillfee(float totalbillfee) {
		this.totalbillfee = totalbillfee;
	}
	public int getTotalbillsum() {
		return totalbillsum;
	}
	public void setTotalbillsum(int totalbillsum) {
		this.totalbillsum = totalbillsum;
	}
	public Map<Integer, Channel> getChannellist() {
		return channellist;
	}
	public void setChannellist(Map<Integer, Channel> channellist) {
		this.channellist = channellist;
	}
	public Map<String, Integer> getTotalmap() {
		return totalmap;
	}
	public void setTotalmap(Map<String, Integer> totalmap) {
		this.totalmap = totalmap;
	}
	public List<KpayKeepAndUserNum> getKeepandusernumlist() {
		return keepandusernumlist;
	}
	public void setKeepandusernumlist(List<KpayKeepAndUserNum> keepandusernumlist) {
		this.keepandusernumlist = keepandusernumlist;
	}
	public KpayFeeData getFeedata() {
		return feedata;
	}
	public void setFeedata(KpayFeeData feedata) {
		this.feedata = feedata;
	}
	public KpayKeepAndUserNum getKeepandusernum() {
		return keepandusernum;
	}
	public void setKeepandusernum(KpayKeepAndUserNum keepandusernum) {
		this.keepandusernum = keepandusernum;
	}
	public List<TerraceInfo> getProviderlist() {
		return providerlist;
	}
	public void setProviderlist(List<TerraceInfo> providerlist) {
		this.providerlist = providerlist;
	}
	public List<AppInfo> getApplist() {
		return applist;
	}
	public void setApplist(List<AppInfo> applist) {
		this.applist = applist;
	}
	public List<AppVersionInfo> getAppversionlist() {
		return appversionlist;
	}
	public void setAppversionlist(List<AppVersionInfo> appversionlist) {
		this.appversionlist = appversionlist;
	}
	public List<Channel> getChannellist1() {
		return channellist1;
	}
	public void setChannellist1(List<Channel> channellist1) {
		this.channellist1 = channellist1;
	}
	public AllQueryData getAlldata() {
		return alldata;
	}
	public void setAlldata(AllQueryData alldata) {
		this.alldata = alldata;
	}
	public String getAppnumber() {
		return appnumber;
	}
	public void setAppnumber(String appnumber) {
		this.appnumber = appnumber;
	}
	public List<AllQueryData> getAlldatalist() {
		return alldatalist;
	}
	public void setAlldatalist(List<AllQueryData> alldatalist) {
		this.alldatalist = alldatalist;
	}
	
	

}
